package com.flowerSystem.controller;

import com.flowerSystem.entity.Shoppingcart;
import com.flowerSystem.pojo.ShoppingcartPojo;
import com.flowerSystem.service.ShoppingcartService;
import com.flowerSystem.util.ResponseResult;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 鲜花遇上牛战士
 * @since 2023-08-28
 */
@RestController
@Slf4j
@RequestMapping("/user")
@CrossOrigin
public class ShoppingcartController {

    @Resource
    private ShoppingcartService shoppingcartService;
    /**
     * 点击产品添加到购物车
     * 前端传：
     *      用户id
     *      商品id
     *      当前产品订单数量orderCount（前端默认传过来的为1，或者后台数据库设置为1）
     * @return
     */
    @PostMapping("/shoppingcart")
    public ResponseResult addShoppingCart(@RequestBody Shoppingcart shoppingcart){
        if(shoppingcart != null){
            boolean save = shoppingcartService.save(shoppingcart);
            if(save){
                return ResponseResult.ok(shoppingcart);
            }
        }
        return ResponseResult.fail();
    }

    /**
     * 进入购物车后
     * 点击 + 号(参数值为1)，该商品数量 +1
     * 点击 - 号(参数值为0)，该商品数量 -1
     * 参数1：购物车表 id
     * 参数2：订单点击事件(1 或 0)
     * 产品数量修改
     * @param
     * @return
     */
//    @PutMapping("shoppingcart/{id}/{orderStatus}")
//    public ResponseResult addOrderCount(@PathVariable("id") int id,@PathVariable("orderStatus") Integer orderStatus){
//        Shoppingcart shoppingcart = null;
//        int rows = 0;
//
//        //点击 + 号
//        if(orderStatus == 1) rows = shoppingcartService.plusOrderCountById(id);
//
//        //点击 - 号
//        if(orderStatus == 0) rows = shoppingcartService.reduceOrderCountById(id);
//
//        //修改成功(row代表被修改的行数)
//        if(rows>0){
//            shoppingcart = shoppingcartService.getOrderCountById(id);
//            int orderCount = shoppingcart.getOrderCount();
//            if(orderCount >= 0) return ResponseResult.ok(shoppingcart);
//        }
//            return ResponseResult.fail();
//    }

    /**
     * 点击 + 号
     * 购物车中的当前产品数量加 1
     * @param
     * @return
     */
    @PutMapping("shoppingcartPlus")
    @Transactional
    public ResponseResult plusOrder(@RequestBody Shoppingcart shoppingcarts){
        int rows = shoppingcartService.plusOrderCountById(shoppingcarts.getId());
        if(rows > 0){
            Shoppingcart shoppingcart = shoppingcartService.getOrderCountById(shoppingcarts.getId());
            int orderCount = shoppingcart.getOrderCount();
            if(orderCount >= 0) return ResponseResult.ok(shoppingcart);
        }
        return ResponseResult.fail();
    }

    /**
     * 点击 - 号
     * 购物车中的当前产品数量减 1
     * @return
     */
    @Transactional
    @PutMapping("shoppingcartReduce")
    public ResponseResult reduceOrder(@RequestBody Shoppingcart shoppingcarts){
        int rows = shoppingcartService.reduceOrderCountById(shoppingcarts.getId());
        if(rows > 0){
            Shoppingcart shoppingcart = shoppingcartService.getOrderCountById(shoppingcarts.getId());
            int orderCount = shoppingcart.getOrderCount();
            if(orderCount >= 0) return ResponseResult.ok(shoppingcart);
        }
        return ResponseResult.fail();
    }


    /**
     * 点击购物车后
     * 获取当前用户购物车中所有的数据
     * 通过用户id 查询当前用户购物车表中的产品信息
     * @return
     */
    @GetMapping("/shoppingcart")
    public ResponseResult orderCountAll(Integer userId){
        List<ShoppingcartPojo> shoppingcartPojo = shoppingcartService.getShopOrderAll(userId);
        return ResponseResult.ok(shoppingcartPojo);
    }


    @GetMapping("/shopping")
    public ResponseResult delShopping(int id,int userId){
        int i = shoppingcartService.delShopping(id, userId);
        if (i>0)return ResponseResult.success("删除成功");
        return ResponseResult.fail();
    }


//    前端传过来的 {"ids":"1,2,3,4,5"}
//    @GetMapping("/shoppingcart")
//    public ResponseResult orderCountAll(Map<String,String> map){
//        List<Shoppingcart> list = new ArrayList<>();
//        if(map != null && !map.isEmpty() && map.size() != 0){
//            //获取前端传过来的json,用字符串
//            String ids = map.get("ids");
//            //解析字符串里面的id，并存入list集合中
//            List<String> collect = Arrays.asList(ids.split(",")).stream().map(s->s.trim()).collect(Collectors.toList());
//            for(String id : collect){
//                log.info("解析后的id:---->"+id);
//                int i = Integer.parseInt(id);
//                Shoppingcart shop = shoppingcartService.getById(i);
//                list.add(shop);
//            }
//            return ResponseResult.ok(list);
//        }
//        return ResponseResult.fail();
//    }
}
